package com.shawn.example.mvvmdemo.home.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.jetpack.mvvm.lib.net.module.HttpApiResult
import com.shawn.example.mvvmdemo.home.repository.WXRepository
import com.shawn.example.mvvmdemo.home.vo.AuthorVO
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class WXViewModel @Inject constructor() : ViewModel() {
    companion object {
        private const val TAG = "WXViewModel"
    }
    @Inject
    lateinit var repository: WXRepository
    private val authorListLiveData = MutableLiveData<List<AuthorVO>>()

    val authorListLV: LiveData<List<AuthorVO>> = authorListLiveData

    fun getArticles() = viewModelScope.launch(Dispatchers.Default) {
        val authorList = repository.getWxAuthorList()
        if (authorList.isSuccess) {
            val list = (authorList as HttpApiResult.Success).value
            val voList = list.map {
                AuthorVO().apply {
                    id = it.id
                    title = it.name
                }
            }
            authorListLiveData.postValue(voList)
        }
    }
}